import { reactive, ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore("userStore", () => {
    let userInfo = reactive(JSON.parse(localStorage.getItem('userInfo') as string) || {})
    const access_token = ref(JSON.parse(localStorage.getItem('access_token') as string) || "")
    const userId = ref(JSON.parse(localStorage.getItem('userId') as string) || "")
    const expires_in = ref(JSON.parse(localStorage.getItem('expires_in') as string) || 0)

    function changeUserInfo(value: any) {
        userInfo = value
        localStorage.setItem('userInfo', JSON.stringify(value))
    }
    function changeAccessToken(value: string) {
        access_token.value = value
        localStorage.setItem('access_token', JSON.stringify(value))
    }
    function changeUserId(value: string) {
        userId.value = value
        localStorage.setItem('userId', JSON.stringify(value))
    }
    function changeExpiresIn(value: string) {
        expires_in.value = value
        localStorage.setItem('expires_in', JSON.stringify(value))
    }

    return { userInfo, access_token, userId, expires_in, changeUserInfo, changeAccessToken, changeUserId, changeExpiresIn }
})